using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;

namespace Grebok.Domain
{
    public class Order : DomainObject
    {
        public Order()
        {
            this.Customer = new UnknownCustomer();
            this.Employee =  new UnknownEmployee();
            this.Items = new List<OrderItem>();
            this.Date = null; ;
        }

        /// <summary>
        /// Creates an order for a given customer
        /// </summary>
        /// <param name="customer">The customer</param>
        /// <returns>An order</returns>
        public static Order CreateOrder(Customer customer)
        {
            if (customer == null)
            {
                throw new ArgumentNullException("customer", "Customer cannot be null");
            }
            Order order = new Order();
            order.Customer = customer;

            return order;
        }

        /// <summary>
        /// Gets or sets the ship name
        /// </summary>
        [StringLengthValidator(40)]
        public virtual string ShipName { get; set; }

        /// <summary>
        /// Gets or sets the shipper information
        /// </summary>
        public virtual Shipper Shipper { get; set; }

        /// <summary>
        /// Gets or sets the Freight
        /// </summary>
        public virtual decimal Freight { get; set; }

        /// <summary>
        /// Gets or sets the shipped date
        /// </summary>
        public virtual DateTime? ShippedDate { get; set; }

        /// <summary>
        /// Gets or sets the date
        /// </summary>
        public virtual DateTime? Date { get; set; }

        /// <summary>
        /// Gets or sets the required date
        /// </summary>
        public virtual DateTime? RequiredDate { get; set; }

        /// <summary>
        /// Gets or sets the order detail
        /// </summary>
        public virtual IList<OrderItem> Items { get; set; }

        /// <summary>
        /// Gets or sets the Id
        /// </summary>
        public virtual int Id { get; set; }

        /// <summary>
        /// Gets or sets the customer
        /// </summary>
        public virtual Customer Customer { get; set; }

        /// <summary>
        /// Gets or sets the employee responsible for the order management
        /// </summary>
        public virtual Employee Employee { get; set; }

        /// <summary>
        /// Gets or sets the address info
        /// </summary>
        public virtual AddressInfo ShipAddressInfo { get; set; }

        /// <summary>
        /// Calculate the base price
        /// </summary>
        /// <returns>The base price</returns>
        public virtual decimal CalculatePrice()
        {
            decimal price = 0;
            foreach (OrderItem item in this.Items)
            {
                price += item.GetPrice();
            }
            return price;
        }
    }
}
